#include "StdAfx.h"
#include "OverlappingCarrier.h"

OverlappingCarrier::OverlappingCarrier(Carrier* carrier, unsigned int overlapLength)
{
	this->carrier = carrier;
	this->overlapLength = overlapLength;
	vocoderListener = NULL;
}

OverlappingCarrier::~OverlappingCarrier(void)
{
}

void OverlappingCarrier::SetVocoderListener(VocoderListener* vocoderListener)
{
	this->vocoderListener = vocoderListener;
}

void OverlappingCarrier::Initialize(unsigned int windowLength)
{
	lastSample.clear();
	curSample.clear();
	for(unsigned int i=0; i<windowLength; i++)
	{
		lastSample.push_back(0);
		curSample.push_back(0);
	}
}

short* OverlappingCarrier::CreateCarrierSample(Settings* settings, short* modulatorSample, unsigned int length, std::vector<SampleMarker>* markers=NULL)
{
	if(lastSample.size()==0)
	{
		Initialize(length);
	}

	unsigned int outputIdx = 0;
	for(unsigned int i=lastSample.size()-overlapLength; i<lastSample.size(); outputIdx++, i++)
	{
		//beginning of output receives overlapping end of last sample
		curSample[outputIdx] = lastSample[i];
		//end of last sample becomes beginning of the next last sample
		lastSample[outputIdx] = lastSample[i];
	}
	std::vector<SampleMarker> curMarkers;
	SampleMarker::GetRegion(0, length-overlapLength-1, *markers, curMarkers);
	short* nextCarrierSample = carrier->CreateCarrierSample(settings, modulatorSample, length-overlapLength, &curMarkers);
	for(unsigned int i=0; outputIdx<length; outputIdx++, i++)
	{
		curSample[outputIdx] = nextCarrierSample[i];
		lastSample[outputIdx] = curSample[outputIdx];
	}
	if(vocoderListener)
	{
		vocoderListener->NotifyCarrierSample(&curSample[0], length);		
	}
	return &curSample[0];
}